<?php
// +----------------------------------------------------------------------
// |     【FabricMain】Model模型类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class FabricMain extends Model{
    use SoftDelete;
    protected $deleteTime = "delete_time";
    
    protected static $prefix;
     
    public function __construct(array $data = []){
        parent::__construct($data);
        self::$prefix = env('database.prefix'); // 获取表名前缀
    }
     
    /**
    * 渠道商一对一关联
    */
    public function supplier(){
        return $this->hasOne(SupplierMain::class, 'id', 'sup_id')->bind([
            'sup_name',
            'exp_method',
            'cut_payment',
            'bulk_payment',
            'exp_name',
        ]);
    }
    
    // 获取列表
    public static function getList($where = []){
        $limit = I('get.limit/d');
        
        //按面料编号查找
        if ($fabric_code = I("fabric_code")) {
           $where[] = ["fabric_code", "like", "%" . $fabric_code . "%"];
        }
        
        //按面料类型查找
        if ($fabric_type = I("fabric_type")) {
            $where[] = ["fabric_type", "=", $fabric_type];
        }
        
        //按面料名称查找
        if ($fabric_name = I("fabric_name")) {
           $where[] = ["fabric_name", "like", "%" . $fabric_name . "%"];
        }
        
        //按供应商ID名称查找
        if ($sup_id = I("sup_id")) {
           $where[] = ["sup_id", "=", $sup_id];
        }

        //按供应编号查找
        if ($sup_code = I("sup_code")) {
           $where[] = ["sup_code", "like", "%" . $sup_code . "%"];
        }
        
        //按备注查找
        if ($remark = I("remark")) {
           $where[] = ["remark", "like", "%" . $remark . "%"];
        }
        
        //是否修改色卡
        if ($ischange = I("is_change")) {
           $where[] = ["is_change", "=",$ischange];
        }

        $list = self::order('id','desc');
        //按供应商名称查找
        if ($sup_name = I("sup_name")) {
           $list = $list->hasWhere('supplier',function($query) use ($sup_name) {
                $query->where([
                    ['sup_name','like',"%" . $sup_name . "%"]
                ]);
           });
        }
        $list = $list->with(['supplier'])->append(['fabric_title'])->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
	
	// 获取列表数据
	public static function listData($where=[],$order="",$n_page=0,$limit=20){
		$data=self::order($order)->where($where);
		if($n_page>0){
			$data=$data->page($n_page,$limit);
		}
		$data=$data->select()->append(['fabric_title'])->toArray();
		
		return $data;
	}
    
    // 获取所有列表数据
    public static function allList($field='',$order="",$where=[]){
        if($field==''){
            $field=[
                'f.*',  // 主表所有字段
                's.sup_name ',
                's.exp_method',
                's.cut_payment',
                's.bulk_payment',
                's.exp_name',
            ];
        }
        // 定义主表和供应商表别名
        $data = self::alias('f')
            ->leftJoin(self::$prefix.'supplier_main s', 'f.sup_id = s.id')
            ->field($field)
            ->order($order)
            ->where($where)
            ->select()
            ->append(['fabric_title'])
            ->toArray();
    
        return $data;
    }

	// 获取某个单条记录
	public static function info($id) {
	    // 构建基础查询 
	    $query = self::with(['supplier']); // 预加载关联 
	 
	    // 根据参数类型查询数据 
	    if (!is_array($id)) {
	        $model = $query->find($id);
	    } else {
	        $model = $query->where($id)->find();
	    }
	 
	    // 追加获取器属性（若模型存在）
	    if ($model) {
	        $model->append(['fabric_title']); // 动态追加虚拟字段 
	    }
	 
	    return $model;
	}
    
    //写入后
    public static function onAfterWrite($model){
    	F('fabric_list',null);			//清除缓存
    }
    
    //删除后
    public static function onAfterDelete($model){
    	F('fabric_list',null);			//清除缓存		
    }
	
	/** 获取器 **/    
    public function getFabricTitleAttr($value){
       $code = $this->getData('fabric_code') ?? '';
       $name = $this->getData('fabric_name') ?? '';

       return $code . ' # ' . $name;
    }

}
